/*
 * Copyright 2008 Robert Hanson <iamroberthanson AT gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.penlets.util;

import java.util.Vector;

/**
 * A collection of utilities for manipulating Strings.
 * @author robert.hanson
 */
public class StringUtils
{

    /**
     * Splits a string into an array of strings.
     * @param str the String to be split
     * @param sep the char separator to split on
     * @param maxNum the maximum number of items in the resulting array
     * @return array of the split parts
     */
    public static String[] split (String str, char sep, int maxNum)
    {
        if (str == null || str.length() == 0) {
            return new String[0];
        }
        
        Vector results = maxNum == 0 ? new Vector() : new Vector(maxNum);
        
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (c == sep) {
                if (maxNum != 0) {
                    if ((results.size() + 1) == maxNum) {
                        for (; i < str.length(); i++) {
                            buf.append(str.charAt(i));
                        }
                        break;
                    }
                }

                results.addElement(buf.toString());
                buf.setLength(0);
            }
            else {
                buf.append(c);
            }
        }

        if (buf.length() > 0) {
            results.addElement(buf.toString());
        }

        return toStringArray(results);
    }

    
    private static String[] toStringArray (Vector strings)
    {
        String[] result = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            result[i] = strings.elementAt(i).toString();
        }
        return result;
    }


    /**
     * Strips a newline from the end of a String.  Strips off
     * ASCII codes 10(LF), 13(CR), and 13+10(CRLF). 
     * @param inStr the String to chomp
     * @return the chomped String
     */
    public static String chomp (String inStr)
    {
        if (inStr == null || "".equals(inStr)) {
            return inStr;
        }

        char lastChar = inStr.charAt(inStr.length() - 1);
        if (lastChar == 13) {
            return inStr.substring(0, inStr.length() - 1);
        }
        else if (lastChar == 10) {
            String tmp = inStr.substring(0, inStr.length() - 1);
            if ("".equals(tmp)) {
                return tmp;
            }
            lastChar = tmp.charAt(tmp.length() - 1);
            if (lastChar == 13) {
                return tmp.substring(0, tmp.length() - 1);
            }
            else {
                return tmp;
            }
        }
        else {
            return inStr;
        }
    }
}
